package com.plant.serviceImpl;

import java.util.List;

import com.plant.dao.BaseDao;
import com.plant.entity.RegisterCode;
import com.plant.entity.UgcDiviceToken;
import com.plant.entity.UgcFriends;
import com.plant.entity.UgcUser;
import com.plant.entity.UgcWeixinUserinf;
import com.plant.service.RegisterService;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service  //依赖注册 必须写注册 下面在写resource资源
@Transactional
public class RegisterServiceImpl implements RegisterService{
    @Resource(name = "baseDaoImpl")
    private BaseDao baseDao;
    

	public boolean add(RegisterCode rg) {	//--1.生成随机四位数验证码
        boolean b=baseDao.add(rg); 
    	return b;        
	}


	public RegisterCode checkUser(RegisterCode rg) {//---2---验证 验证码是否正确
		String hql = "from RegisterCode where tel = :tel and randnomNum = :randnomNum";
		RegisterCode u = null;
		@SuppressWarnings("rawtypes")
		List list = baseDao.findname(hql, new String[]{"tel","randnomNum"}, new Object[]{rg.getTel(), rg.getRandnomNum()});
		if (list!=null&&list.size()>0) {
			u = (RegisterCode) list.get(0);
		}
		return u;
	}
	
	public UgcUser checktel(RegisterCode rg) {//---2验证数据库是否有手机号
		String hql = " from UgcUser where tel = :tel";
		UgcUser u = null;
		@SuppressWarnings("rawtypes")
		List list = baseDao.findname(hql, new String[]{"tel"}, new Object[]{rg.getTel()});
		if (list!=null&&list.size()>0) {
			u = (UgcUser) list.get(0);
		}
		return u;
	}

	public void update(RegisterCode rg) {  //---3修改state状态
		String hql="update RegisterCode set state=1 where id=("+rg.getId()+")";
		baseDao.updatehql(hql);
	}


	public List<Object> getBytel(RegisterCode rg) {
		String hql="from UgcUser where tel=?";
		return baseDao.find(hql, rg.getTel());
	}


	@SuppressWarnings("unchecked")
	public List<Object> searchtel(RegisterCode rg) {    //-----通过 用户id查找用户的手机号码
		String hql="select tel from UgcUser";
		return baseDao.find(hql);
	}


	public boolean addtel(UgcUser user) {      //----添加用户信息到用户手机号和密码 
		return baseDao.add(user);
	}


	public UgcUser checkUser(UgcUser user) {		//---通过 手机号 和用户密码验证 用户是否登录
		String hql="from UgcUser where tel= :tel and password= :password";
		UgcUser u=null;
		@SuppressWarnings("rawtypes")
		List list=baseDao.findname(hql, new String[]{"tel","password"}, new Object[]{user.getTel(), user.getPassword()});
		if (list!=null&&list.size()>0) {
			u = (UgcUser) list.get(0);
		}
		return u;
	}


	public boolean UpdatePass(UgcUser user) {
		return baseDao.update(user);
	}
	
    public boolean addfriend(UgcFriends uf) {//---为新用户添加六个默认好友
        return baseDao.add(uf);
    }


	public boolean AddDiviceToken(UgcDiviceToken ud) {//--添加DiviceToken
		return baseDao.add(ud);
	}


	public UgcUser CheckOpenId(UgcUser user) {
		String hql="from UgcUser where weixinOpenId =:weixinOpenId";
		UgcUser u=null;
		@SuppressWarnings("rawtypes")
		List list=baseDao.findname(hql, new String[]{"weixinOpenId"}, new Object[]{user.getWeixinOpenId()});
		if (list!=null&&list.size()>0) {
			u = (UgcUser) list.get(0);
		}
		return u;
	}


	public List<Object> GetByOpenId(UgcUser user) {
		String hql="from UgcUser where weixinOpenId=?";
		return baseDao.find(hql, user.getWeixinOpenId());
	}


	public boolean AddWeiXinUser(UgcWeixinUserinf uwf) {
		return baseDao.add(uwf);
	}


	@SuppressWarnings("rawtypes")
	public UgcUser CheckUnionId(UgcUser user) {
		String hql="from UgcUser where weixinUnionId =:weixinUnionId";
		UgcUser u=null;
		List list=baseDao.findname(hql, new String[]{"weixinUnionId"}, new Object[]{user.getWeixinUnionId()});
		if (list!=null&&list.size()>0) {
			u = (UgcUser) list.get(0);
		}
		return u;
	}
	
}
